package com.qding.park.common.utils.rchl;



import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class RchlDESede
{
  public static String encryptStringBase64(String sText, String base64Key)
  {
    try
    {
      BASE64Decoder dec = new BASE64Decoder();
      byte[] kb = dec.decodeBuffer(base64Key);

      SecretKeySpec k = new SecretKeySpec(kb, "DESede");

      Cipher cp = Cipher.getInstance("DESede");
      cp.init(1, k);

      byte[] b = sText.getBytes("UTF8");
      byte[] b2 = cp.doFinal(b);

      BASE64Encoder enc = new BASE64Encoder();
      return enc.encodeBuffer(b2);
    }
    catch (Exception e) {
    }
    return null;
  }

  public static String decryptStringBase64(String base64Text, String base64Key)
  {
    try
    {
      BASE64Decoder dec = new BASE64Decoder();
      byte[] kb = dec.decodeBuffer(base64Key);

      SecretKeySpec k = new SecretKeySpec(kb, "DESede");

      Cipher cp = Cipher.getInstance("DESede");
      cp.init(2, k);

      byte[] c = dec.decodeBuffer(base64Text);
      byte[] b = cp.doFinal(c);

      return new String(b, "UTF8");
    }
    catch (Exception e) {
    }
    return null;
  }
}